<?php 
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_rs_viewcat = $GLOBALS['NUMTOVIEW'];
$pg = 0;
if (isset($_GET['pg'])) {
  $pg = $_GET['pg'];
}
$startRow_rs_viewcat = $pg * $maxRows_rs_viewcat;

$rs_viewcat = getCategoryFindList($startRow_rs_viewcat,"NO");
$row_rs_viewcat = mysql_fetch_assoc($rs_viewcat);

if (isset($_GET['tt'])) {
  $tt = $_GET['tt'];
} else {
  $all_rs_viewcat =getCategoryFindList($startRow_rs_viewcat,"YES");
  $tt = mysql_num_rows($all_rs_viewcat);
}
$totalPages_rs_viewcat = ceil($tt/$maxRows_rs_viewcat)-1;

$queryString_rs_viewcat = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pg") == false && 
        stristr($param, "tt") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs_viewcat = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs_viewcat = sprintf("&tt=%d%s", $tt, $queryString_rs_viewcat);

if($tt>0){
?>
<form id="frmCatView" name="frmCatView" method="post" action="">
  <label>  </label>
  <table border="0">
    <tr>
      <td><?php if ($pg > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pg=%d%s", $currentPage, 0, $queryString_rs_viewcat); ?>">First</a>
        <?php } // Show if not first page ?></td>
      <td><?php if ($pg > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pg=%d%s", $currentPage, max(0, $pg - 1), $queryString_rs_viewcat); ?>">Previous</a>
        <?php } // Show if not first page ?></td>
      <td><?php if ($pg < $totalPages_rs_viewcat) { // Show if not last page ?>
        <a href="<?php printf("%s?pg=%d%s", $currentPage, min($totalPages_rs_viewcat, $pg + 1), $queryString_rs_viewcat); ?>">Next</a>
        <?php } // Show if not last page ?></td>
      <td><?php if ($pg < $totalPages_rs_viewcat) { // Show if not last page ?>
        <a href="<?php printf("%s?pg=%d%s", $currentPage, $totalPages_rs_viewcat, $queryString_rs_viewcat); ?>">Last</a>
        <?php } // Show if not last page ?></td>
      <td>Records <?php echo ($startRow_rs_viewcat + 1) ?> to <?php echo min($startRow_rs_viewcat + $maxRows_rs_viewcat, $tt) ?> of <?php echo $tt ?></td>
      <td><label>
        <input type="submit" name="button" id="button" value="Approve" onclick="document.getElementById('frmaction').value='approve'" />
      </label>
        <input type="submit" name="button2" id="button2" value="Delete" onclick="document.getElementById('frmaction').value='delete'; return confirm('Do YOU really want to delete this Item as it might delete other sub Item');" />
        <input type="hidden" name="frmaction" id="frmaction" />
        <input name="frmCont" type="hidden" id="frmCont" value="category" />
      <input name="chkprefix" type="hidden" id="chkprefix" value="delchk" /></td>
    </tr>
  </table>
  <table width="100%" border="0" cellpadding="0" cellspacing="0" id="datatable">
    <thead class="fixedHeader">
    <tr >
      <th width="30%" align="left">Category Name</th>
      <th width="19%" align="left">Show</th>
      <th width="37%" align="left">Parent </th>
      <th width="14%" align="center">Action
        <input type="checkbox" name="delchk_0" id="delchk_0" onchange="checkUncheck('frmCatView','delchk')" /></th>
      
    </tr>
    </thead>
    <tbody>
    <?php  do { ?>
    <tr>
      <td><a href="?page=category&amp;itemid=<?php echo $row_rs_viewcat['category_id']; ?>"><?php echo $row_rs_viewcat['category_name']; ?></a></td>
      <td><?php echo ($row_rs_viewcat['is_published']); ?></td>
      <td><a href="pages/category_update.php?aj&amp;itemid=<?php echo $row_rs_viewcat['parent_id']; ?>" rel="facebox"><?php echo getCategoryName($row_rs_viewcat['parent_id']); ?></a></td>
      <td align="center"><label>
        <input type="checkbox" name="delchk_<?php echo $row_rs_viewcat['category_id'];?>" id="delchk_<?php echo  $row_rs_viewcat['category_id'];?>"  value="<?php echo $row_rs_viewcat['category_id']; ?>"/>
      </label></td>
    </tr>
    <?php } while ($row_rs_viewcat = mysql_fetch_assoc($rs_viewcat)); ?>
 </tbody>
  </table>
</form>
<?php

} else {?>
<div id="error">No Category Yet</div>
    <?php } mysql_free_result($rs_viewcat);?>
